//
//  HDCameraTopBar.swift
//  HuaDaoPeifu
//
//  Created by Erickson on 16/3/31.
//  Copyright © 2016年 paiyipai. All rights reserved.
//

import UIKit

protocol HDCameraTopBarDelegate:class {
    func didSelectTorch()
    func didSelectCancel()
}

class HDCameraTopBar: UIView {

    weak var delegate:HDCameraTopBarDelegate?
    let cancel = UIButton()

    init() {
        super.init(frame: CGRect.zero)
//        self.backgroundColor = HDCameraColor.topBarColor
        if AppConstant.screenHeight == 480 {
            self.backgroundColor = UIColor.clear
        }
        
        setupViews()
    }
    
    func setupViews() {
        
        
        let statusBar = UIView()
        statusBar.backgroundColor = HDCameraColor.topBarColor
        self.addSubview(statusBar)
        statusBar.snp.makeConstraints { (make) in
            make.top.left.right.equalTo(self)
            make.height.equalTo(20)
        }
        
        let contentView = UIView()
        contentView.backgroundColor = HDCameraColor.topBarColor
        self.addSubview(contentView)
        contentView.snp.makeConstraints { (make) in
            make.edges.equalTo(UIEdgeInsetsMake(20, 0, 0, 0))
        }
        
        
        
//        let btnY = (self.frame.size.height - 20)/2
        let torch = UIButton()
        torch.setImage(UIImage(named: "HD_camera_flash_off"), for: UIControlState())
        torch.setImage(UIImage(named: "HD_camera_flash_on"), for: .selected)
        torch.addTarget(self, action: #selector(torchAction(_:)), for: .touchUpInside)
        contentView.addSubview(torch)
        torch.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.centerY.equalTo(contentView)
            make.width.equalTo(torch.currentImage!.size.width+10)
            make.height.equalTo(torch.currentImage!.size.height+10)
        }
        
        
        cancel.setImage(UIImage(named: "camera_cancle"), for: UIControlState())
        cancel.addTarget(self, action: #selector(cancelAction(_:)), for: .touchUpInside)
        contentView.addSubview(cancel)
        cancel.snp.makeConstraints { (make) in
            make.right.equalTo(-15)
            make.centerY.equalTo(contentView)
            make.width.equalTo(cancel.currentImage!.size.width+10)
            make.height.equalTo(cancel.currentImage!.size.height+10)
        }
        
        
        
    }
    
    func showCancelButton() {
        cancel.isHidden = false
    }
    func hiddenCancelButton() {
        cancel.isHidden = true
    }
    func torchAction(_ sender:UIButton) {
        delegate?.didSelectTorch()
        sender.isSelected = !sender.isSelected

    }
    
    
    func cancelAction(_ sender:UIButton) {
        delegate?.didSelectCancel()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
